package com.enterprise.projectManager.resource;

import com.enterprise.projectManager.dto.LoginRequest;
import com.enterprise.projectManager.dto.RefreshTokenRequest;
import com.enterprise.projectManager.dto.SignUpRequest;
import com.enterprise.projectManager.model.Response;
import com.enterprise.projectManager.service.implemention.AuthenticationServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import static java.time.LocalDateTime.now;
import static java.util.Map.of;
import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.OK;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/auth")
public class AuthenticationResource {

    private final AuthenticationServiceImpl authenticationService;

    @GetMapping("/test")
    public ResponseEntity<String> test()
    {
        return ResponseEntity.ok("/api/v1/auth test");
    }

    @PostMapping("/signup")
    public ResponseEntity<Response> signup(@RequestBody SignUpRequest signUpRequest)
    {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("newUser",authenticationService.signUp(signUpRequest)))
                        .message("注册用户")
                        .status(CREATED)
                        .statusCode(CREATED.value())
                        .build()
        );
    }


    @PostMapping("/login")
    public ResponseEntity<Response> login(@RequestBody LoginRequest loginRequest)
    {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("jwtAuthenticationResponse",authenticationService.login(loginRequest)))
                        .message("用户登录")
                        .status(CREATED)
                        .statusCode(CREATED.value())
                        .build()
        );
    }

    @PostMapping("/refresh")
    public ResponseEntity<Response> refresh(@RequestBody RefreshTokenRequest refreshTokenRequest)
    {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("refreshToken",authenticationService.refreshToken(refreshTokenRequest)))
                        .message("refresh")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }
}
